﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.Extension;
using Infrastructure.Log;

namespace ZICModel.Fault
{
    public class MainFaultDetail
    {
        public string Code { get; set; }//故障系统代号（如ECU、发动机系统）
        public string Version { get; set; }//故障码库号
        public int SubFaultCount { get; set; } //子系统的故障个数
        private string srcProeprtyDataString;

        public List<SubFaultDetail> ZISSubFaultList { get; set; }

        public MainFaultDetail(string srcString)
        {
            srcProeprtyDataString = srcString;
            ZISSubFaultList = new List<SubFaultDetail>();
        }

        public string Process()
        {
            Code = srcProeprtyDataString.Substring(0, 2);
            Version = srcProeprtyDataString.Substring(2, 2);
            SubFaultCount = srcProeprtyDataString.Substring(4, 2).HexString2Int();

            CallLogger.LogMessage(typeof(MainFaultDetail),string.Format("\tMainFaultCode:{0} MainFaultVersion:{1},SubFaultCount:{2}",
                                                            Code,
                                                            Version,
                                                            SubFaultCount));
            
            int start = 0;
            int faultStart = 0;
            while (start < SubFaultCount)
            {
                faultStart = 6 + start * 8; //8个字符的字符串，前6个是故障码， 2 两个是状态
                SubFaultDetail subFaultDetail = new SubFaultDetail(srcProeprtyDataString.Substring(faultStart, 8), Version);
                subFaultDetail.Process();
                ZISSubFaultList.Add(subFaultDetail);
                start++; // move to the next SubFault
            }

            return srcProeprtyDataString.Substring(faultStart + 8); // finish current MainFault, return left over string for next MainFault
        }
    }
}
